#ifndef USART_DRIVER_H
#define USART_DRIVER_H
#include "stdint.h"
#include "gd32f10x.h"
#include "gd32f10x_usart.h"
#include "string.h"
#include "stdio.h"
#include "stdbool.h"
#include "gd32f10x_misc.h"




#define SHORT_FILE strrchr(__FILE__, '\\') ? (strrchr(__FILE__, '\\') + 1) : (__FILE__)
#define log(prefix, fmt, ...) printf("[%s][%s][%s,%d] " fmt "\r\n", prefix, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define DEBUG_ERROR(fmt, ...) log("A", "****** " fmt " ******", ##__VA_ARGS__)
#define DEBUG_LOG(fmt, ...) log("B", fmt, ##__VA_ARGS__)



extern uint16_t uart0_preLen;
extern uint8_t uart0_sbuf[];
extern uint8_t uart0_rbuf[];
extern uint16_t uart1_preLen;
extern uint8_t uart1_sbuf[];
extern uint8_t uart1_rbuf[];
extern uint16_t uart2_preLen;
extern uint8_t uart2_sbuf[];
extern uint8_t uart2_rbuf[];
extern uint16_t uart3_preLen;
extern uint8_t uart3_sbuf[];
extern uint8_t uart3_rbuf[];
extern uint16_t uart4_preLen;
extern uint8_t uart4_sbuf[];
extern uint8_t uart4_rbuf[];

extern bool uart0_recv_finish;
extern bool uart1_recv_finish;
extern bool uart2_recv_finish;
extern bool uart3_recv_finish;
extern volatile bool uart4_recv_finish;






void usart_send_long_data(uint32_t UARTx, u8* data_buf, u16 data_len);


void uart0clear(void);
void uart1clear(void);
void uart2clear(void);
void uart3clear(void);
void uart4clear(void);


void uart0_send_buf_clear(void);

void uart3_send_buf_clear(void);

void uart4_send_long_data(uint8_t* data_buf, uint16_t data_len);
void uart1_send_long_data(uint8_t* data_buf, uint16_t data_len);
void uart3_send_long_data(uint8_t* data_buf, uint16_t data_len);

void uart0_init(uint32_t baud, uint8_t bits, char verify, uint8_t stopbit);
void uart1_init(uint32_t baud, uint8_t bits, char verify, uint8_t stopbit);
void uart2_init(uint32_t baud, uint8_t bits, char verify, uint8_t stopbit);
void uart3_init(uint32_t baud, uint8_t bits, char verify, uint8_t stopbit);
void uart4_init(uint32_t baud, uint8_t bits, char verify, uint8_t stopbit);

void dbg_uart_send(uint8_t* data, uint32_t len);
void print_hex_to_str(uint8_t* data, uint32_t len);


#endif
